/*
 * LangResource.java
 * 
 * This file is part of Netuno Psamata.
 * 
 * CajuScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3, or (at your option) 
 * any later version.
 * 
 * CajuScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CajuScript.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.netuno.psamata;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Locale;

/**
 * Lang Resource.
 * @author eduveks
 */
public class LangResource {
	private List<LangResource> extras = new ArrayList<LangResource>();
    /**
     * Resource Bundle.
     */
    private ResourceBundle bundle = null;
    /**
     * Name.
     */
    private String resourceName = null;
    /**
     * Locale.
     */
    private Locale resourceLocale = null;
    /**
     * Lang Resource.
     * @param name Name
     * @param locale Locale
     */
    public LangResource(final String name, final String locale) {
        load(name, new Locale(locale));
    }
    /**
     * Lang Resource.
     * @param name Name
     * @param locale Locale
     */
    public LangResource(final String name, final Locale locale) {
        load(name, locale);
    }
    /**
     * Get.
     * @param key Key
     * @return value
     */
    public final String get(final String key) {
    	if (bundle.containsKey(key)) {
    		return bundle.getString(key);
    	} else {
    		for (LangResource langResource : extras) {
    			if (!langResource.get(key).isEmpty()) {
    				return langResource.get(key);
    			}
    		}
    	}
        return "";
    }
    /**
     * Get.
     * @param key Key
     * @param formats Formats
     * @return value
     */
    public final String get(final String key, final Object... formats) {
        return String.format(get(key), formats);
    }
    /**
     * Get Name.
     * @return name
     */
    public final String getName() {
        return resourceName;
    }
    /**
     * Get Locale.
     * @return locale
     */
    public final Locale getLocale() {
        return resourceLocale;
    }
    /**
     * Get Bundle.
     * @return Resource Bundle
     */
    public final ResourceBundle getResourceBundle() {
        return bundle;
    }
    /**
     * Load.
     * @param name Name
     * @param locale Locale
     */
    public final void load(final String name, final String locale) {
        load(name, new Locale(locale));
    }
    /**
     * Load.
     * @param name Name
     * @param locale Locale
     */
    public final void load(final String name, final Locale locale) {
        resourceName = name;
        resourceLocale = locale;
        bundle = ResourceBundle.getBundle(name, locale);
    }
    
    public final void addExtra(LangResource langResource) {
    	extras.add(langResource);
    }
    
    public final void removeExtra(LangResource langResource) {
    	extras.remove(langResource);
    }
}
